import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import router from './router'
import store from './store'
import VueCookies from 'vue-cookies'
import axios from 'axios'
import md5 from "js-md5"
import { isAuth } from "./utils"

// 测试数据，在接入后台服务后要删除
import TestData from './TestData'
Vue.prototype.$testData = TestData
Vue.prototype.$md5 = md5;
Vue.prototype.isAuth = isAuth;


Vue.prototype.$cookies = VueCookies;
// 本地调试
axios.defaults.baseURL = "http://localhost:9999";
// 生产环境
// axios.defaults.baseURL="http://47.93.203.21/ruby-api"
// 内网调试
// axios.defaults.baseURL="http://192.168.50.24";
// 是否携带cookie但是感觉好像用不上 
// axios.defaults.withCredentials = true;
// 在所有请求中统一添加authorization
axios.interceptors.request.use(config => {
    if (Vue.prototype.$cookies.get("authorization")) {
        config.headers.authorization = Vue.prototype.$cookies.get("authorization");
    }
    config.headers.workbench = "PC_ADMIN";
    return config;
})

axios.interceptors.response.use(function (config) {
    if (config.data.respCode == 401) {
        router.replace("/login")
        return;
    }
    /* response响应的结果有很多东西，包括：
        config,data,headers,request,status,statusText
        而业务数据都在data里面，所以将config.data直接返回，这样数据处理起来方便一点*/
    if (config.headers.authorization) {
        config.data.authorization = config.headers.authorization;
    }
    return config.data;
}, function (error) {
    if (error.response.status == 401) {
        router.replace("/login")
    }
})


// 做登录拦截，没有登录的就直接返回到登录页面
router.beforeEach((to, from, next) => {
    console.log()
    if (!Vue.prototype.$cookies.get("authorization")) {
        if (to.path != "/login") {
            return next('/login')
        }
    }
    next();
})




Vue.use(ElementUI)

Vue.prototype.$http = axios;



Vue.config.productionTip = false

new Vue({
    render: h => h(App),
    router,
    store
}).$mount('#app')
